﻿Imports <xmlns:log="urn:myLogConfig">
Imports System.Xml.Schema
Imports System.Xml.Schema.Extensions
Imports System.Windows.Media.Effects

Public Class plateformeUC

    Friend maPlateforme As Plateforme
    Friend treeViewSender As TreeViewItemWithIcons

    Private Sub BTN_save_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles BTN_save.Click
        'obtenir la fenetre principale
        Dim r = MainWindow.moiMeme

        Try
            'Rechercher le noeud de la plateforme
            Dim pltXML = (From ele In r.masterDoc...<log:plateforme> _
                            Select ele Where ele.@nom = maPlateforme.nom).First

            pltXML.@nom = TBX_nom.Text
            pltXML.@domain = TBX_domaine.Text
            pltXML.@login = TBX_login.Text
            pltXML.@password = TBX_password.Text
            pltXML.@destination = TBX_destination.Text

            Me.maPlateforme.reFill(pltXML)

            'Mettre à jour l'arbre
            Me.treeViewSender.Tag = maPlateforme
            Me.treeViewSender.tagXML = pltXML
            Me.treeViewSender.HeaderText = maPlateforme.nom

            'Sauvegarder les changement dans le fichier
            r.masterDoc.Save(r.xmlFileName)

            'Afficher message succes
            r.consoleOut.addItim(New ConsoleTypes.ok("Modification de la plateforme: Succès", pltXML.@nom))

        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de la modification de la plateforme", ex.Message))
        End Try
    End Sub

    Friend Sub setInfo(plt As Plateforme, sender As TreeViewItemWithIcons)
        maPlateforme = plt
        treeViewSender = sender

        TBX_nom.Text = maPlateforme.nom
        TBX_domaine.Text = maPlateforme.domain
        TBX_login.Text = maPlateforme.login
        TBX_password.Text = maPlateforme.password
        TBX_destination.Text = maPlateforme.destination
    End Sub

    Private Sub BTN_plateformeDelete_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles BTN_plateformeDelete.Click
        'obtenir la fenetre principale
        Dim r = MainWindow.moiMeme
        'Rechercher le noeud de la plateforme
        Dim queryPlt = (From ele In r.masterDoc...<log:plateforme> _
                        Select ele Where ele.@nom = maPlateforme.nom).First

        Dim allserv = From srv In queryPlt.<log:serveur>
                      Select srv

        If allserv.Count = 0 Then
            If MessageBox.Show("Supprimer la plateforme " & queryPlt.@nom & " , continuer ?", "Confirmation", MessageBoxButton.YesNo) = MessageBoxResult.Yes Then

                Try
                    queryPlt.Remove()
                    r.panelDynamic.Children.Clear()

                    'Sauvegarder les changement dans le fichier
                    r.masterDoc.Save(r.xmlFileName)

                    'recharger treeView
                    r.readXML()

                    'Afficher message succes
                    r.consoleOut.addItim(New ConsoleTypes.ok("Suppression de la plateforme: Succès", queryPlt.@nom))

                Catch ex As Exception
                    r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de la suppression de la plateforme", ex.Message))
                End Try

            End If

        Else
            MessageBox.Show("Vous ne pouvez pas supprimer la plateforme " & queryPlt.@nom & " car elle contient des serveurs.")
        End If

    End Sub

    Private Sub BTN_serverAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles BTN_serverAdd.Click

        'obtenir la fenetre principale
        Dim r = MainWindow.moiMeme

        Try

            'Rechercher le noeud de la plateforme
            Dim queryPlt = (From ele In r.masterDoc...<log:plateforme> _
                            Select ele Where ele.@nom = maPlateforme.nom).First

            Dim newServNode As New XElement(XElement.Parse(xmlTemplates.newServer))
            newServNode.SetAttributeValue("xmlns", Nothing)
            queryPlt.Add(newServNode)

            'Sauvegarder les changement dans le fichier
            r.masterDoc.Save(r.xmlFileName)

            Dim unServeur As New Serveur(newServNode, maPlateforme)

            Dim noeudServeur As New TreeViewItemWithIcons
            noeudServeur.Icon = My.Resources.server.ToImageSource
            noeudServeur.HeaderText = unServeur.nom
            noeudServeur.Tag = unServeur
            noeudServeur.tagXML = newServNode
            treeViewSender.Items.Add(noeudServeur)

            treeViewSender.IsExpanded = True
            noeudServeur.IsSelected = True
            noeudServeur.BringIntoView()

            'Afficher message succes
            r.consoleOut.addItim(New ConsoleTypes.ok("Ajout du serveur: Succès", newServNode.@nom))

        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de l'ajout du serveur", ex.Message))
        End Try

    End Sub

    Private Sub BTN_plateformeAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles BTN_plateformeAdd.Click

        'obtenir la fenetre principale
        Dim r = MainWindow.moiMeme

        Try

            'Rechercher le noeud data
            Dim queryData = (From ele In r.masterDoc...<log:data>).First

            Dim newPltNode As New XElement(XElement.Parse(xmlTemplates.newPateforme))
            newPltNode.SetAttributeValue("xmlns", Nothing)
            queryData.Add(newPltNode)

            'Sauvegarder les changement dans le fichier
            r.masterDoc.Save(r.xmlFileName)

            Dim unePlateforme As New Plateforme(newPltNode)

            Dim noeudPlateforme As New TreeViewItemWithIcons
            noeudPlateforme.Icon = My.Resources.plateforme.ToImageSource
            noeudPlateforme.HeaderText = unePlateforme.nom
            noeudPlateforme.Tag = unePlateforme
            r.TreeView1.Items.Add(noeudPlateforme)

            noeudPlateforme.IsSelected = True
            noeudPlateforme.BringIntoView()

            'Afficher message succes
            r.consoleOut.addItim(New ConsoleTypes.ok("Ajout de la plateforme: Succès", newPltNode.@nom))

        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de l'ajout de la plateforme", ex.Message))
        End Try

    End Sub

    Private Sub plateformeUC_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Dim blur As New BlurEffect
        'blur.Radius = 5
        'TBX_password.Effect = blur

    End Sub
End Class
